<template>
	<view>
		<view class="bg head">
			<view v-if="tabData.length>0">
				<scroll-view scroll-x="true" class="scroll-view">
					<view class="scroll-item" v-for="(item,index) in tabData" :key="index"
						:class="tabIndex==item.id?'active':''" @click="changeIndex(index,item)">
						{{item.gameName}}
					</view>
				</scroll-view>
			</view>
			<ren-dropdown-filter :filterData='filterData' :border="false" :defaultIndex='defaultIndex'
				@onSelected='change' class="u-skeleton-rect">
			</ren-dropdown-filter>
		</view>

		<view class="margin-lr-sm margin-top-16" v-if="dataList.length" style="margin-top: 180upx;">
			<view class="flex justify-between radius bg padding-sm margin-top-16" @click="goOrder(item)"
				v-for="(item,index) in dataList" :key='index'>
				<image :src="item.homepageImg?item.homepageImg: '../../../static/logo.png'"
					style="width: 200rpx;height: 200rpx;border-radius: 10rpx;" mode="aspectFill"></image>
				<view class="flex-sub margin-left text-white flex flex-direction justify-between">
					<view class="flex align-center">
						<image src="../../../static/images/qi.png" style="width: 80rpx;height: 40rpx;"></image>
						<view class="margin-right-xs" style="display: inline-block;margin-left: 10rpx;width: 340rpx;">
							{{item.myLevel}}
						</view>
					</view>

					<view class="flex radius margin-top-sm" style="line-height: 34upx;">
						<view class="margin-right-xs" style="background: #FBEBF0;color: #FF2D01;padding: 10upx 16upx;">
							距离{{item.distance}}km
						</view>
						<!-- <view class="margin-right-xs" style="background: #FBEBF0;color: #FF2D01;padding: 10upx 16upx;">
							{{item.gameId[0]}}
						</view> -->
						<view class="margin-right-xs" style="background: #FBEBF0;color: #FF2D01;padding: 10upx 16upx;">
							{{item.serviceMethod === '1'?'上门服务':'到店服务'}}
						</view>
					</view>
					<!-- <view style="color:#FF1200;font-size: 24rpx;font-family: PingFang-SC-Bold;letter-spacing: 2rpx;">
						<view>
							<text style="font-size: 38rpx;font-weight: bold;">{{isVip? item.memberMoney :item.money}}</text>元/<text>{{item.unit}}</text>
						</view>
					</view> -->
					<view class="flex align-center justify-between margin-top-sm" style="width: 100%;">
						<!-- <view
							style="color:#FF1200;font-size: 24rpx;font-family: PingFang-SC-Bold;letter-spacing: 2rpx;">
							<text
								style="font-size: 38rpx;font-weight: bold;">{{isVip? item.memberMoney :item.money}}</text>元/<text>{{item.unit}}</text>
						</view> -->
						<view class="price-container">
							<text class="current-price">
								¥
								<text style="font-size: 18px;">{{ item.money }}</text>
							</text>
							<text class="discount-tag">{{discountPercentage(item.money,item.oldMoney)}}</text>
							<text class="original-price">¥{{ item.oldMoney }}</text>
						</view>
						<view class="text-gray text-sm">
							已服务{{item.count}}次
						</view>
					</view>
					<!-- <view style="width: 100%;display: flex;justify-content: space-between;align-items: center;">
						<view class="flex align-center" @click.stop="shop(item.userId)">
							<view class="margin-right-xs">{{item.shopName}}</view>
							<image src="../../../static/images/index/right.png" style="width: 14upx;height: 23upx;">
							</image>
						</view>
						<view style="color: #999999;font-size: 26upx;" v-if="item.salesNum">
							已售{{item.salesNum}}
						</view>
					</view> -->
					<view class="flex align-center justify-between margin-top-sm" style="width: 100%;">
						<view></view>
						<view class="xiad1">立即预约</view>
					</view>
				</view>
			</view>
		</view>

		<empty v-if="!dataList.length"></empty>
	</view>
</template>

<script>
	import empty from '@/components/empty.vue'
	export default {
		components: {
			empty
		},
		data() {
			return {
				page: 1,
				limit: 10,
				dataList: [],
				isVip: false,
				myId: '',
				city: '',
				defaultIndex: [0, 0, 0],
				filterData: [
					[{
							label: '距离优先',
							value: 3,
						},
						{
							label: '销量优先',
							value: 2,
						}, {
							label: '同城',
							value: 1,
						}
					],
					[{
							label: '销量',
							value: '',
						},
						{
							label: '从高到低',
							value: 'desc',
						},
						{
							label: '从低到高',
							value: 'asc',
						}
					],
					[{
							label: '价格',
							value: '',
						},
						{
							label: '从高到低',
							value: 'desc',
						},
						{
							label: '从低到高',
							value: 'asc',
						}
					],
				],
				value1: '3',
				value2: '',
				value3: '',
				id: '',
				tabData: [],
				tabIndex: 0,
				parentId: "",
				classparentId: '',
				isRm: '',
			}
		},
		computed: {
			discountPercentage() {
				return function(money, oldMoney) {
					let discount = money / oldMoney;
					let discountPercentage = (Math.ceil(discount * 100) / 10).toFixed(1);
					return discountPercentage + "折";
				}
			}
		},
		onLoad(option) {
			this.isVip = uni.getStorageSync('isVIP') ? uni.getStorageSync('isVIP') : false
			this.myId = uni.getStorageSync('userId')
			this.city = uni.getStorageSync('city')
			uni.setNavigationBarTitle({
				title: option.name
			})
			if (option.name) {
				this.id = option.name
			}
			
			if(option.isRm){
				console.log("testsetsetset")
				this.getQueryEjGameSort();
				if(option.classparentId){
					this.classparentId = option.classparentId
				}
				this.isRm = true;
				this.getDataList();
			}else{
				if (option.parentId) {
					this.parentId = option.parentId
					this.getClassfly()
				}
				
				if (option.classparentId) {
					this.tabIndex = option.classparentId
					this.classparentId = option.classparentId
					this.getDataList()
				} else {
					this.classparentId = option.parentId
					this.getDataList()
				}
			}
		},
		methods: {
			/**
			 * 获取二级分类排序列表
			 */
			getQueryEjGameSort() {
				this.$Request.get('/app/appGame/queryEjGameSort').then(res => {
					this.tabData = res.data;
				})
			},
			// 跳转店铺
			shop(userId) {
				uni.navigateTo({
					url: '/pages/index/game/shop?shopUserId=' + userId
				})
			},
			changeIndex(index, item) {
				this.tabIndex = item.id
				this.classparentId = item.id
				this.page = 1
				this.isRm = false;
				this.getDataList()
			},
			// 获取类型
			getClassfly() {
				let _this = this
				this.$Request.get("/app/appGame/queryGameName?parentId=" + this.parentId).then(res => {
					if (res.code == 0) {
						_this.tabData = res.data
						// _this.classparentId = res.data[0].id
						// this.getDataList()
					}
				});
			},
			// 筛选
			change(e) {

				this.value1 = e[0][0].value
				this.value2 = e[1][0].value
				this.value3 = e[2][0].value
				this.getDataList()
				// this.mescroll.resetUpScroll()
			},
			getDataList() {
				let data = {
					id: this.classparentId,
					page: this.page,
					limit: this.limit,
					isRm: this.isRm,
					// id: this.id,
					condition: this.value1, //智能优选
					salesNum: this.value2, //不限男女
					by: this.value3, //价格
					latitude: uni.getStorageSync('latitude'),
					longitude: uni.getStorageSync('longitude'),
					userId: uni.getStorageSync('userId') ? uni.getStorageSync('userId') : '',
					city: uni.getStorageSync('city'),
				}
				this.$Request.get("/app/appGame/selectServiceList", data).then(res => {
					if (res.code == 0 && res.data) {
						if (this.page == 1) {
							for (let i = 0; i < res.data.records.length; i++) {
								if (res.data.records[i].distance) {
									res.data.records[i].distance = (res.data.records[i].distance / 1000).toFixed(1)
								}
								if (res.data.records[i].gameId) {
									res.data.records[i].gameId = res.data.records[i].gameId.split(",");
								}
							}
							this.dataList = res.data.records
						} else {
							for (let i = 0; i < res.data.records.length; i++) {
								if (res.data.records[i].distance) {
									res.data.records[i].distance = (res.data.records[i].distance / 1000).toFixed(1)
								}
								if (res.data.records[i].gameId) {
									res.data.records[i].gameId = res.data.records[i].gameId.split(",");
								}
							}
							this.dataList = [...this.dataList, ...res.data.records]

						}
					}
					uni.stopPullDownRefresh();
				});
			},

			// 跳转订单
			goOrder(e) {
				let token = uni.getStorageSync('token')
				if (token) {
					uni.navigateTo({
						url: '/pages/index/game/order?id=' + e.id
					});
				} else {
					uni.navigateTo({
						url: '/pages/public/login'
					});
				}
			},
		},
		onReachBottom: function() {
			this.page = this.page + 1;
			this.getDataList();
		},
		onPullDownRefresh: function() {
			this.page = 1;
			this.getDataList();
		},
	}
</script>

<style>
	page {
		background-color: #f7f7f7;
	}

	.bg {
		background: #ffffff;
	}

	.head {
		position: fixed;
		/* #ifdef H5 */
		top: 80rpx;
		padding-top: 30rpx;
		/* #endif */
		/* #ifndef H5 */
		top: 0rpx;
		/* #endif */
		left: 0;
		right: 0;
		z-index: 99;
	}

	.xiad1 {
		/* width: 200rpx; */
		/* height: 60upx; */
		/* margin: 0 auto; */
		background: #557EFD;
		border-radius: 49upx;
		/* line-height: 60upx; */
		text-align: center;
		color: #FFFFFF;
		padding: 12rpx 30rpx;

	}

	.scroll-view {
		width: 100%;
		height: 60upx;
		white-space: nowrap;
		background: #ffffff;
		padding-left: 30upx;
	}

	.scroll-item {
		display: inline-block;
		width: 180upx;
		height: 60upx;
		background: #F7F7F7;
		margin-right: 30upx;
		border-radius: 55upx;
		text-align: center;
		line-height: 60upx;
	}

	.active {
		background: #E5F4FF;
		color: #51B5FF;
	}

	.price-container {
		display: flex;
		align-items: center;
	}

	.original-price {
		font-size: 14px;
		color: #999;
		/* 添加删除线 */
		text-decoration: line-through;
	}

	.discount-tag {
		font-size: 10px;
		color: #ff5000;
		border: 1px solid;
		padding: 1px 3px;
		border-radius: 3px;
		margin-right: 5px;

	}

	.current-price {
		font-size: 14px;
		color: #ff5000;
		font-weight: bold;
		margin-right: 3px;
	}
</style>